﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class FIM_ExpenseRates : System.Web.UI.UserControl
    {
        double sumBath = 0;
        Main main = new Main();
        IBLL_FIM_MAS_ExpenseRates ExpRate = new B_FIM_MAS_ExpenseRates();
        //IBLL_FIM_MAS_ExpenseRateTypes ExpRateType = new B_FIM_MAS_ExpenseRateTypes();
        //IBLL_FIM_MAS_ExpenseRateTypeGroups ExpRateTypeGrp = new B_FIM_MAS_ExpenseRateTypeGroups();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtBgmExpSeq.Text = Request.QueryString["Seq"];
                txtBgmSourceSeq.Text = Request.QueryString["BgmSourceSeq"];
                LoadDataFimExpenseRate();
                cmdOK.OnClientClick = string.Format("returnToParent('{0}', '{1}')", txtSumBathAmount.ClientID
                    , txtBgmExpSeq.ClientID);
            }

            SetSummary();
        }

        protected void SetSummary()
        {
            double dAmount = 0;
            double dQuantity = 0;
            double dBathAmount = 0;
            double dSumBathAmount = 0;

            RadNumericTextBox txtAmount = null;
            RadNumericTextBox txtQuantity = null;
            RadNumericTextBox txtBathAmount = null;

            for (int i = 0; i < grdFimExpenseRate.Items.Count; i++)
            {
                txtAmount = (RadNumericTextBox)grdFimExpenseRate.Items[i]["quantity"].FindControl("txtAmount");
                txtQuantity = (RadNumericTextBox)grdFimExpenseRate.Items[i]["quantity"].FindControl("txtQuantity");
                txtBathAmount = (RadNumericTextBox)grdFimExpenseRate.Items[i]["bath_amount"].FindControl("txtBathAmount");

                if (txtAmount.Text.Length > 0)
                    dAmount = Convert.ToDouble(txtAmount.Text);
                else
                    dAmount = 0;

                if (txtQuantity.Text.Length > 0)
                    dQuantity = Convert.ToDouble(txtQuantity.Text);
                else
                    dQuantity = 0;

                dBathAmount = dAmount * dQuantity;
                txtBathAmount.Text = dBathAmount.ToString();
                dSumBathAmount += dBathAmount;
            }

            txtSumBathAmount.Value = dSumBathAmount;
        }

        protected void LoadDataFimExpenseRate()
        {
            string strBgmExpSeq = txtBgmExpSeq.Text;
            string strBgmSourceSeq = txtBgmSourceSeq.Text;
            string strSeq;
            //string[] strExpRateTypeSeq = new string[RepeaterDtls.Items.Count];
            int j = 0;

            //for (int i = 0; i < strExpRateTypeSeq.Length; i++)
            //{
            //    RadComboBox ddl = (RadComboBox)RepeaterDtls.Items[i].FindControl("ddlExpRateTypeSeq");
            //    strSeq = ddl.SelectedValue;

            //    if (strSeq.Length > 0)
            //    {
            //        strExpRateTypeSeq[j] = strSeq;
            //        j++;
            //    }
            //}

            grdFimExpenseRate.DataSource = ExpRate.GetFull(strBgmExpSeq, strBgmSourceSeq);
            grdFimExpenseRate.DataBind();
        }

        protected void RepeaterDtls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item is RepeaterItem)
            {
                if (e.Item.ItemType != ListItemType.Header)
                {
                    //RepeaterItem dataItem = (RepeaterItem)e.Item;
                    //Label lbl = (Label)dataItem.FindControl("lblExpRateTypeGrpSeq");
                    //RadComboBox ddl = (RadComboBox)dataItem.FindControl("ddlExpRateTypeSeq");

                    //ddl.DataSource = ExpRateType.GetList(lbl.Text);
                    //ddl.DataBind();

                    //main.InsertEmptyDDLAll(ddl);
                    //ddl.SelectedIndex = 0;
                }
            }
        }

        protected void ddlExpRateTypeSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataFimExpenseRate();
        }

        protected void grdFimExpenseRate_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = (GridDataItem)e.Item;
                RadNumericTextBox txtAmount = (RadNumericTextBox)dataItem["quantity"].FindControl("txtAmount");
                RadNumericTextBox txtQuantity = (RadNumericTextBox)dataItem["quantity"].FindControl("txtQuantity");
                RadNumericTextBox txtBathAmount = (RadNumericTextBox)dataItem["bath_amount"].FindControl("txtBathAmount");

                txtAmount.Attributes.Add("OnBlur", string.Format("txtBlur('{0}', '{1}', '{2}')"
                    , txtQuantity.ClientID, txtAmount.ClientID, txtBathAmount.ClientID));
                txtAmount.Attributes.Add("OnFocus", string.Format("txtFocus('{0}', '{1}', '{2}')"
                    , txtQuantity.ClientID, txtAmount.ClientID, txtBathAmount.ClientID));
                txtQuantity.Attributes.Add("OnBlur", string.Format("txtBlur('{0}', '{1}', '{2}')"
                    , txtQuantity.ClientID, txtAmount.ClientID, txtBathAmount.ClientID));
                txtQuantity.Attributes.Add("OnFocus", string.Format("txtFocus('{0}', '{1}', '{2}')"
                    , txtQuantity.ClientID, txtAmount.ClientID, txtBathAmount.ClientID));
                txtQuantity.Text = "1";

                string strBathAmount = txtBathAmount.Text;
                if (strBathAmount.Length == 0)
                    strBathAmount = "0";
                sumBath += double.Parse(strBathAmount);
            }
        }
    }
}